﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LP580.CG
{
    /// <summary>
    /// clsCompanySupport 的摘要说明
    /// </summary>
    public class clsCompanySupport
    {
        #region 构造函数

        public clsCompanySupport() { }

        public clsCompanySupport(int prmUserID)
        {
            clsCompanySupport entity = clsCompanySupport.GetByKey(prmUserID);
            this._userid = entity._userid;
            this._galleryqq = entity._galleryqq;
            this._dutyqq = entity._dutyqq;
            this._complaint = entity._complaint;
        }

        #endregion

        public static clsCompanySupport GetByKey(int prmUserID)
        {
            clsCompanySupport entity = new clsCompanySupport();
            DBase dbo = new DBase();
            System.Data.IDataReader dr = dbo.ExecuteReader("SELECT * FROM [tbl_CompanySupport] WHERE UserID=" + prmUserID.ToString());
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        private static clsCompanySupport DrToClass(System.Data.IDataReader dr)
        {
            clsCompanySupport entity = new clsCompanySupport();
            int intTmp = 0;
            if (!int.TryParse(dr["UserID"].ToString(), out intTmp))
                intTmp = 0;
            entity._userid = intTmp;
            entity._galleryqq = dr["GalleryQQ"].ToString();
            entity._dutyqq = dr["DutyQQ"].ToString();
            entity._complaint = dr["Complaint"].ToString();
            return entity;
        }

        #region 增删改数据库操作

        public static int Insert(clsCompanySupport prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_CompanySupport](" +
                            " [UserID]," +
                            " [GalleryQQ]," +
                            " [DutyQQ]," +
                            " [Complaint]" +
            ") values (" +
                    "@UserID," +
                    "@GalleryQQ," +
                    "@DutyQQ," +
                    "@Complaint" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@GalleryQQ", prmEntity._galleryqq);
            SaveDTM.AddNewParam("@DutyQQ", prmEntity._dutyqq);
            SaveDTM.AddNewParam("@Complaint", prmEntity._complaint);
            SaveDTM.Insert();
            return 1;
        }

        public static bool Delete(int prmUserID)
        {
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_CompanySupport] WHERE UserID=" + prmUserID.ToString());
            dbo.CloseConn();
            dbo = null;
            return true;
        }

        public static bool Delete(clsCompanySupport prmEntity)
        {
            return clsCompanySupport.Delete(prmEntity._userid);
        }

        public static bool Update(clsCompanySupport prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = "UPDATE [tbl_CompanySupport] SET " +
                "[GalleryQQ] = @GalleryQQ, " +
                "[DutyQQ] = @DutyQQ, " +
                "[Complaint] = @Complaint " +
            " WHERE UserID=@UserID";
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@GalleryQQ", prmEntity._galleryqq);
            SaveDTM.AddNewParam("@DutyQQ", prmEntity._dutyqq);
            SaveDTM.AddNewParam("@Complaint", prmEntity._complaint);
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.Update();
            return true;
        }

        #endregion

        public static List<clsCompanySupport> GetCollection()
        {
            List<clsCompanySupport> ls = new List<clsCompanySupport>();
            DBase dbo = new DBase();
            string sql = "SELECT * FROM [tbl_CompanySupport] ORDER BY UserID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        public static bool HasUser(int uid)
        {
            DBase db = new DBase();
            object o = db.ExecuteScalar("select userid FROM [tbl_CompanySupport] where userid=" + uid.ToString());
            db = null;
            return o != null;
        }

        #region 字段属性

        private int _userid = 0;
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private string _galleryqq = "";
        /// <summary>
        /// 展厅客服
        /// </summary>
        public string GalleryQQ
        {
            get { return _galleryqq; }
            set { _galleryqq = value; }
        }

        private string _dutyqq = "";
        /// <summary>
        /// 值班客服
        /// </summary>
        public string DutyQQ
        {
            get { return _dutyqq; }
            set { _dutyqq = value; }
        }

        private string _complaint = "";
        /// <summary>
        /// 投诉建议
        /// </summary>
        public string Complaint
        {
            get { return _complaint; }
            set { _complaint = value; }
        }

        #endregion
    }
}
